﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using BL;
using DB;

namespace Database
{
	public class TableItemMapper : IMapper
	{
		public TableItemMapper()
		{
		}

		#region IMapper Members

		public List<Entity> LoadAll()
		{
			SubjectItemMapper sim = new SubjectItemMapper();
			List<Entity> list = new List<Entity>();
			DataTable dt = AccessDb.Instance.GetTable("TableItem");
			foreach (DataRow row in dt.Rows)
			{
                TableItem ti;
				SubjectItem si = (SubjectItem)sim.Find(Convert.ToInt64(row["subject_id"]));
                if (si == null)
                    continue;
                try
                {
                        ti = new TableItem(Convert.ToInt64(row["id"]), (Weeks)row["week"],
                        (Days)row["day"], (int)row["pair"], si);
                }
                catch
                {
                    continue;
                }
				list.Add((Entity)ti);
			}
			return list;
		}

		public Entity Find(long id)
		{
			SubjectItemMapper sim = new SubjectItemMapper();
			DataTable dt = AccessDb.Instance.GetTable("TableItem");
			foreach (DataRow row in dt.Rows)
			{
                try
                {
                    if ((long)row["id"] == id)
                    {
                        SubjectItem si = (SubjectItem)sim.Find(Convert.ToInt64(row["subject_id"]));
                        return new TableItem(Convert.ToInt64(row["id"]), (Weeks)row["week"],
                            (Days)row["day"], (int)row["pair"], si);
                    }
                }
                catch
                {
                    continue;
                }
			}
			return null;
		}

		public void Insert(Entity e)
		{
            try
            {
                TableItem item = (TableItem)e;
                DataTable dt = AccessDb.Instance.GetTable("TableItem");
                DataRow row = dt.NewRow();
                row["id"] = item.ID;
                row["day"] = (int)item.Day;
                row["week"] = (int)item.Week;
                row["pair"] = item.Pair;
                row["subject_id"] = item.Item.ID;
                dt.Rows.Add(row);
            }
            catch { }
		}

		public void DeleteEntity(Entity e)
		{
			DataRow r = null;
			DataTable dt = AccessDb.Instance.GetTable("TableItem");
			foreach (DataRow row in dt.Rows)
			{
                try
                {
                    if (Convert.ToInt64(row["id"]) == e.ID)
                        r = row;
                }
                catch
                {
                    continue;
                }
			}
            if (r != null)
			    r.Delete();
		}

		public void UpdateEntity(Entity e)
		{
			DataRow r = null;
			DataTable dt = AccessDb.Instance.GetTable("TableItem");
			foreach (DataRow row in dt.Rows)
			{
                try
                {
                    if (Convert.ToInt64(row["id"]) == e.ID)
                        r = row;
                }
                catch
                {
                    continue;
                }
			}
			TableItem item = (TableItem)e;
            if (r == null)
                return;
			r["id"] = item.ID;
			r["day"] = (int)item.Day;
			r["week"] = (int)item.Week;
			r["pair"] = item.Pair;
			r["subject_id"] = item.Item.ID;
		}

		public void ClearAll()
		{
			DataTable dt = AccessDb.Instance.GetTable("TableItem");
			dt.Rows.Clear();
		}

		#endregion
	}
}
